#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

#include "all.h"
#include "opencv2/opencv.hpp"
#include "debug.h"
#include "plateJudge.h"
#include "mserLocate.h"
#include "preprocess2.h"
#include "characterSegment.h"
#include "CCharacter.h"
#include "characterIdentify.h"
#include "characterSegIdentify.h"
#include "annTrain.h"
#include "colorLocate.h"
#include "CCharacter.h"

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
   //rename_iter(argv[1]);
/*
    std::vector<std::string> vecStr = getFiles("./test/18");
    vector<string>::iterator it = vecStr.begin();
    for(;it != vecStr.end(); ++it){
    	string name = *it;
        cout << name << endl;
        Mat Pre_src_image = imread(name);
        vector<Mat> outPlatesMatMser, outPlatesMatColor;
    	Mat dest_image2, dest_image3;

    	resizeAndCut(Pre_src_image, dest_image2, false);
        resizeAndCut(Pre_src_image, dest_image3, true);
    	plateMserLocate(dest_image2, outPlatesMatMser);
    	plateMserLocate(dest_image3, outPlatesMatMser);
        //showAndDestroy(dest_image2);
        //showAndDestroy(dest_image3);

    	vector<Plate> inVec;
        {
        	vector<Mat>::iterator it = outPlatesMatMser.begin();
        	for(;it != outPlatesMatMser.end(); ++it){
        		Mat pMat = *it;
        		Plate plate;
        		plate.plateMat = pMat;
        		plate.fMser = true;
                preprocessPlate(plate, true);
        		inVec.push_back(plate);
        	}
        }
    	colorLocate(dest_image2, outPlatesMatColor);
        colorLocate(dest_image3, outPlatesMatColor);

        {
            vector<Mat>::iterator it = outPlatesMatColor.begin();
        	for(; it != outPlatesMatColor.end(); ++it){
        		Mat pMat = *it;
        		Plate plate;
        		plate.plateMat = pMat;
        		plate.fMser = false;
        		inVec.push_back(plate);
        	}
        }
        
    	Plate resultPlate;
        cutAndWrite(outPlatesMatMser, "./train/svm");
        cutAndWrite(outPlatesMatColor, "./train/svm_train_data");
        Pre_src_image.release();
        //showAndDestroy(outPlatesMatMser);
    }
*/


    //rename_iter(argv[1]);
/*
    std::vector<std::string> vecStr = getFiles(argv[1]);
    vector<string>::iterator it = vecStr.begin();
    for(;it != vecStr.end(); ++it){
    	vector<Mat> outPlatesMatMser, outPlatesMatColor;
    	Mat dest_image2, dest_image3;

        Mat Pre_src_image = imread(*it);

    	resizeAndCut(Pre_src_image, dest_image2, false);
        resizeAndCut(Pre_src_image, dest_image3, true);

    	plateMserLocate(dest_image2, outPlatesMatMser);
    	plateMserLocate(dest_image3, outPlatesMatMser);
        //showAndDestroy(dest_image2);
        //showAndDestroy(dest_image3);

    	vector<Plate> inVec;
        {
        	vector<Mat>::iterator it = outPlatesMatMser.begin();
        	for(;it != outPlatesMatMser.end(); ++it){
        		Mat pMat = *it;
        		Plate plate;
        		plate.plateMat = pMat;
        		plate.fMser = true;
                preprocessPlate(plate, true);
        		inVec.push_back(plate);
        	}
        }
    	colorLocate(dest_image2, outPlatesMatColor);
        colorLocate(dest_image3, outPlatesMatColor);

        {
            vector<Mat>::iterator it = outPlatesMatColor.begin();
        	for(; it != outPlatesMatColor.end(); ++it){
        		Mat pMat = *it;
        		Plate plate;
        		plate.plateMat = pMat;
        		plate.fMser = false;
        		inVec.push_back(plate);
        	}
        }
        
    	Plate resultPlate;
        //showAndDestroy(outPlatesMatMser);
        //showAndWrite(outPlatesMatMser, "./train/svm_train_data");
        PlateJudge::instance()->plateJudgeUsingNMS(inVec, resultPlate);
        cout << resultPlate.fMser << endl;
    	//showAndDestroy(resultPlate.plateMat);
        vector<Mat> resultVec;
    	charsSegment(resultPlate.plateMat, resultVec, BLUE);
        //size_t size = resultVec.size();

        cutAndWrite(resultVec, "./train/charSeg", *it);
        Pre_src_image.release();
    	//for(size_t index = 0; index < size; index++){
    		//cutAndWrite(resultVec[index], ss.str());
    	//}
        //showAndDestroy(resultVec);
    }
*/
/*
	vector<Mat> outPlatesMatMser, outPlatesMatColor;
	Mat dest_image2, dest_image3;

    Mat Pre_src_image = imread(argv[1]);

	resizeAndCut(Pre_src_image, dest_image2, false);
    resizeAndCut(Pre_src_image, dest_image3, true);

	plateMserLocate(dest_image2, outPlatesMatMser);
	plateMserLocate(dest_image3, outPlatesMatMser);
    //showAndDestroy(dest_image2);
    //showAndDestroy(dest_image3);

	vector<Plate> inVec;
    {
    	vector<Mat>::iterator it = outPlatesMatMser.begin();
    	for(;it != outPlatesMatMser.end(); ++it){
    		Mat pMat = *it;
    		Plate plate;
    		plate.plateMat = pMat;
    		plate.fMser = true;
            preprocessPlate(plate, true);
    		inVec.push_back(plate);
    	}
    }
	colorLocate(dest_image2, outPlatesMatColor);
    colorLocate(dest_image3, outPlatesMatColor);

    {
        vector<Mat>::iterator it = outPlatesMatColor.begin();
    	for(; it != outPlatesMatColor.end(); ++it){
    		Mat pMat = *it;
    		Plate plate;
    		plate.plateMat = pMat;
    		plate.fMser = false;
    		inVec.push_back(plate);
    	}
    }
    
	Plate resultPlate;
    //showAndDestroy(outPlatesMatMser);
    //showAndWrite(outPlatesMatMser, "./train/svm_train_data");
    PlateJudge::instance()->plateJudgeUsingNMS(inVec, resultPlate);
    cout << resultPlate.fMser << endl;
	//showAndDestroy(resultPlate.plateMat);
    vector<Mat> resultVec;
	charsSegment(resultPlate.plateMat, resultVec, BLUE);
    showAndDestroy(resultVec);

    vector<CCharacter> charCandidateVecChinese, charCandidateVecChar;
    {
        vector<Mat>::iterator it = resultVec.begin();
    	for(; it != resultVec.end(); ++it){
    		Mat pMat = *it;
            if(it == resultVec.begin()){
            	CCharacter charCandidateAdap;
                charCandidateAdap.setCharacterMat(pMat);
                charCandidateVecChinese.push_back(charCandidateAdap);
            }
            else
            {
                CCharacter charCandidateAdap;
                charCandidateAdap.setCharacterMat(pMat);
                charCandidateVecChar.push_back(charCandidateAdap);
            }
    	}
    }
   
    CharsSegIdentify::instance()->classifyChinese(charCandidateVecChinese);
    CharsSegIdentify::instance()->classify(charCandidateVecChar);
    string result = charCandidateVecChinese[0].getCharacterStr();
    vector<CCharacter>::iterator it = charCandidateVecChar.begin();
    for(; it != charCandidateVecChar.end(); ++it){
        CCharacter cchar = *it;
        result += cchar.getCharacterStr();
    }
    cout << "result: " << result << endl;
*/

    //Mat Pre_src_image = imread(argv[1]);
    //Mat dImage;
    //resizeAndCut(Pre_src_image, dImage, LITTLE, true);
    //showAndDestroy(dImage);
	plateMserCutForAnnTrain("./base_pic", LITTLE);
    //iterationSort();



    
    //size_t size = resultVec.size();

    return 0;
}
